<?php
require_once("../../class2.php");
require_once("./includes/require_all.php");

if (in_array(USERNAME,$mods,true)) {
	$fulltext=$_POST['text'];
		$fulltext= str_replace("\\n", "\n", $fulltext);
		$translation= explode("\n", $fulltext);
		foreach ($translation as $temp) {
			$temp= explode("=>", $temp);
			$textid[]= $temp[0]; $translated[]= str_replace("✘", "\\n", $temp[1]);
		}

	$prefix= $_GET['game']; $id= $_GET['id'];
	$mysql= $prefix."_text"; $mysql2= $prefix."_translations";
	$additionals= mysql_fetch_array(mysql_query("SELECT file_name, translator_name, page FROM $mysql2 WHERE id='$id'"));
	$translator= $additionals['translator_name']; $file= $additionals['file_name']; $view_page= $additionals['page'];

	foreach ($translated as $i=>$content) {
		$text_id= $textid[$i]; $count++;
		if (preg_match("/(IC101: Остави английския текст)/", $content)) {
			$content= str_replace("(IC101: Остави английския текст)", "", $content);
			$text_id= str_replace("_IGcoLLen", "", $text_id);
			$text_id.= "_IGcoLLen"; }
		if (preg_match("/_IGcoLLen/", $text_id)>0 AND preg_match("/[а-яА-Я]{2,}/u", $content)>0) {
			$text_id= str_replace("_IGcoLLen", "", $text_id); }
		mysql_query("UPDATE $mysql SET content='".mysql_real_escape_string($content)."', text_id='".mysql_real_escape_string($text_id)."', translator=IF(translator='original', '$translator', translator) WHERE text_id='".mysql_real_escape_string($textid[$i])."' and file_name='$file'");
		
		//translating all the same rows
		$x = mysql_query("select * from $mysql where text_id = '$textid[$i]' and file_name = '$file'");
		$y = mysql_fetch_array($x);
		$idd = $y['text_id'];
		$eng = $y['english'];
		$bg = $y['content'];
		$tr = $y['translator'];
		$rrr = mysql_query("select * from $mysql where english = binary '".mysql_real_escape_string($eng)."' and translator = 'original'");
		if(mysql_num_rows($rrr)){
			if(preg_match("/(_IGcoLLen)/", $idd)){
				mysql_query("UPDATE $mysql SET text_id = concat(text_id, '_IGcoLLen'), content = '".mysql_real_escape_string($bg)."', translator = '".mysql_real_escape_string($tr)."' WHERE english = binary '".mysql_real_escape_string($eng)."' and translator = 'original'");
			}
			else{
				mysql_query("UPDATE $mysql SET content = '".mysql_real_escape_string($bg)."', translator = '".mysql_real_escape_string($tr)."' WHERE english = binary '".mysql_real_escape_string($eng)."' and translator = 'original'");
			}
		}
	}

	$fulltext= str_replace("✘", "\\n", $fulltext);
	mysql_query("UPDATE $mysql2 SET translation='".mysql_real_escape_string($fulltext)."', status='Одобрен' WHERE id='$id'");
	CMS_lastApprovedSave($count, $translator);

	unset($fulltext,$translation,$temp,$textid,$translated);
	
	$wordsall=mysql_fetch_array(mysql_query("SELECT SUM( LENGTH(english) - LENGTH(REPLACE (english, ' ', ''))+1) FROM $text_table"));
	$wordsdone= mysql_fetch_array(mysql_query("SELECT SUM( LENGTH(english) - LENGTH(REPLACE (english, ' ', ''))+1) FROM $text_table WHERE content REGEXP '[а-яА-Я]{1,}'"));
	mysql_query("UPDATE admin_panel SET wordsall='$wordsall[0]', wordsdone='$wordsdone[0]' WHERE game_prefix='$CMS_pref'");
	
	if (isset($_GET['updat'])) {
		echo "<div id='dummy'></div><script>$('#dummy').load('viewtext.php?game=$_GET[updat]&name=$file&page=$view_page'+' '+'#dummy');</script>";
	}
}
?>